package com.hontech.kaltaapp.device

import com.hontech.kaltaapp.device.codec.CodecType
import com.hontech.kaltaapp.exception.ExecException
import com.hontech.kaltaapp.utils.toU8

class RecvFrame(private val buf: ByteArray) {


    fun type() = buf[0].toU8()
    fun seq() = buf[1].toU8()
    fun cmd() = buf[2].toU8()
    fun ec() = buf[3].toU8()

    fun parseRes(vararg args: CodecType) {
        if (ec() != 0) {
            throw ExecException(execErrMsg(ec()))
        }
        var index = 4
        for (arg in args) {
            arg.decode(buf, index)
            index += arg.size()
        }
    }

    fun parseNotify(vararg args: CodecType) {
        var index=  3
        for (arg in args) {
            arg.decode(buf, index)
            index += arg.size()
        }
    }
}





